/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ace.game;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 *
 * @author TimotiusNC
 */
public class Score {
	public Score(String username, int score){
		m_username	= username;
		m_score		= score;
	}

	//METHODS
    public static void sortOneScoreArray(Score[] oneScore) {
        int maxIdx = 0;

        for(int i=0; i < oneScore.length-1; i++) {
            maxIdx = i;
            for(int j=i+1; j<oneScore.length; j++) { //get the highest score
                if(oneScore[j].getScore() > oneScore[maxIdx].getScore()) { //swap
                    maxIdx = j;
                }
            }
            String tempUsername = oneScore[i].getUsername();
            int tempScore = oneScore[i].getScore();

            oneScore[i].setUsername(oneScore[maxIdx].getUsername());
            oneScore[i].setScore(oneScore[maxIdx].getScore());

            oneScore[maxIdx].setUsername(tempUsername);
            oneScore[maxIdx].setScore(tempScore);
        }
    }

    public void fromByteArray(byte[] data) throws IOException {
        ByteArrayInputStream bin = new ByteArrayInputStream(data);
        DataInputStream din = new DataInputStream(bin);

        m_username	= din.readUTF();
        m_score		= din.readInt();

        din.close();
    }

    public byte[] toByteArray() throws IOException {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        DataOutputStream dout = new DataOutputStream(bout);

        dout.writeUTF(m_username);
        dout.writeInt(m_score);

        dout.close();

        return bout.toByteArray();
    }

	//ACCESSORS
	public String getUsername(){
		return m_username;
	}

	public void setUsername(String username){
		m_username = username;
	}

	public int getScore(){
		return m_score;
	}

	public void setScore(int score){
		m_score = score;
	}

	//PROPERTIES
	private String	m_username;
	private int		m_score;
}
